#include "test.h"
#include "key.h"
#include "relay.h"
#include "systime.h"
#include "adc_driver.h"
#include "qDebug.h"
#include "pir_sensor_detect.h"
//#include "interrupts_hc32f002.h"
#include "stdio.h"
#include "flash_act.h"
#include "timeout_conf.h"
#include "sensor_cfg.h"
#include "light_cfg.h"

typedef enum
{
    TEST_STATE_INIT,
    TEST_STATE_ONE,
    TEST_STATE_TWO,
    TEST_STATE_THREE,
    TEST_STATE_FOUR,
}TEST_STATE_E;


static uint8 relay_state;

static TEST_STATE_E test_state;
static timestamp_t   test_rtc;


void led_init(void);
void set_led(uint8 sw);
void ac_zero_init(void);

void test_io_interrupt(void);



void test_init(void)
{
   
     
    set_relay_switch(OFF);
    relay_state = 0x00;
    
    test_state = TEST_STATE_INIT;
}




//按键控制开关
void test_task(void)
{
    if(is_key_click()){
        relay_state ^= 0x01;
        set_relay_switch(relay_state);
        set_led(relay_state);
    }
   

}


//小灯闪烁，验证systime
void test1_task(void)
{
    switch(test_state){
        case TEST_STATE_INIT:
            set_led(ON);
            relative_time_add_ms(&test_rtc,0);
            test_state = TEST_STATE_ONE;
            break;
        case TEST_STATE_ONE:
            if(is_relative_time_timeout(&test_rtc))
            {
                set_led(ON);
//                relative_time_add_ms(&test_rtc,100);
                relative_time_add_sec(&test_rtc,10);
                test_state = TEST_STATE_TWO;
            }
            break;
        case TEST_STATE_TWO:
            if(is_relative_time_timeout(&test_rtc))
            {
                set_led(OFF);
//                relative_time_add_ms(&test_rtc,50);
                relative_time_add_sec(&test_rtc,5);
                test_state = TEST_STATE_ONE;
            }
            
        default:
            break;
            
    
    
    }

}

static volatile uint16 adc_val;
//静态ADC and 动态ADC采集验证
void test2_task(void)
{
    switch(test_state){
        case TEST_STATE_INIT:
            relative_time_add_ms(&test_rtc,0);
            test_state = TEST_STATE_ONE;
            break;
        case TEST_STATE_ONE:
            if(is_relative_time_timeout(&test_rtc))
            {
//                adc_val = get_time_val();
//                qDebug(0xA1);
//                qDebug(adc_val>>8);
//                qDebug(adc_val);
//                qDebug('\n');
                
//                adc_val = get_light_val();
//                qDebug(0xA2);
//                qDebug(adc_val>>8);
//                qDebug(adc_val);
//                qDebug('\n');
//                
//                adc_val = get_pir_sensor_sensitivity_val();
//                qDebug(0xA3);
//                qDebug(adc_val>>8);
//                qDebug(adc_val);
//                qDebug('\n');                
//                
//                
//                adc_val = get_photoD_val();
//                qDebug(0xA4);
//                qDebug(adc_val>>8);
//                qDebug(adc_val);
//                qDebug('\n');

                adc_val = get_pir_val();
                qDebug(0xA5);
                qDebug(adc_val>>8);
                qDebug(adc_val);
                qDebug('\n');
                
                relative_time_add_ms(&test_rtc,20);
            }
            break;
        default:
            break;
    
    
    }

}


static uint8_t cnt = 0;
void test4_task(void)
{
    cnt++;
    printf("the cnt value is %d \r\n",cnt);
}


static uint16_t write_val = 0;
static uint16_t read_val = 0;
void test5_task(void)
{
    write_val++;
    write_user_halfword(write_val);
    printf("write: %x \r\n",write_val);
    read_user_halfword(&read_val);
    printf("read: %x \r\n",read_val);
    if(write_val != read_val){
        printf("-----------------ERROR---------------------\r\n");
    }
}

//#define WRITE_ONLY
void test6_task(void)
{
    write_val = 1171;
#ifdef WRITE_ONLY
    write_user_halfword(write_val);
    printf("write: %d \r\n",write_val);
#else
    read_user_halfword(&read_val);
    printf("read: %d \r\n",read_val);
    if(write_val != read_val){
        printf("-----------------ERROR---------------------\r\n");
    }
#endif
}

#define BLINK_TIME  20
uint8_t led_sw;
void test7_task(void)
{
    switch(test_state)
    {
        case TEST_STATE_INIT:
            set_led(ON);
            led_sw = 1;
            relative_time_add_ms(&test_rtc,BLINK_TIME);
            test_state = TEST_STATE_ONE;
            break;
        case TEST_STATE_ONE:
            if(is_relative_time_timeout(&test_rtc)){
                led_sw ^= 0x01;
                set_led(led_sw);
                relative_time_add_ms(&test_rtc,BLINK_TIME);
            }
            break;
        
        default:
            break;
    
    }
}

void test8_task(void)
{
    switch(test_state)
    {
        case TEST_STATE_INIT:
            set_led(ON);
            led_sw = 1;
            relative_time_add_sec(&test_rtc,BLINK_TIME);
            test_state = TEST_STATE_ONE;
            break;
        case TEST_STATE_ONE:
            if(is_relative_time_timeout(&test_rtc)){
                led_sw ^= 0x01;
                set_led(led_sw);
                relative_time_add_sec(&test_rtc,BLINK_TIME);
            }
            break;
        
        default:
            break;
    
    }
}


void test9_task(void)
{
    
     if(is_key_click()){
        printf("key click! \r\n");
     }
     if(is_key_long_press()){
        printf("the key long press!\r\n");
     }
     
     
     switch(test_state){
         case TEST_STATE_INIT:
             set_key_switch(KEY_ENABLE);
             test_state = TEST_STATE_ONE;
             relative_time_add_sec(&test_rtc,30);
             break;
         case TEST_STATE_ONE:
             if(is_relative_time_timeout(&test_rtc)){
                relative_time_add_sec(&test_rtc,30);
                set_key_switch(KEY_DISABLE); 
                test_state = TEST_STATE_TWO;
                printf("disable the key! \r\n");
             }
             break;
         case TEST_STATE_TWO:
             if(is_relative_time_timeout(&test_rtc)){
                relative_time_add_sec(&test_rtc,30);
                set_key_switch(KEY_ENABLE); 
                test_state = TEST_STATE_ONE;
                printf("enable the key! \r\n");
             }
             break;
         
         
         default:
             break;
     
     }
}

static TIMEOUT_LEVEL   time_cfg_sec;
static brightness_sensor_mode_e  light_cfg;
static ENUM_SENSOR  sensor_cfg;
void test10_task(void)     //!todo 装整机后进行测试
{
     switch(test_state){
         case TEST_STATE_INIT:
             test_state = TEST_STATE_ONE;
             relative_time_add_sec(&test_rtc,30);
             break;
         case TEST_STATE_ONE:
             if(is_relative_time_timeout(&test_rtc)){
                relative_time_add_sec(&test_rtc,30);
                time_cfg_sec = timeout_conf_get_sec();
                printf("the time_cfg:%d",time_cfg_sec);
                light_cfg = get_brightness_sensor_mode();
                printf("the light_cfg:%d",light_cfg);
                sensor_cfg = get_pir_sensor_sensitivity();
                printf("the sensor_cfg:%d",sensor_cfg);
             }
             break;
         
         default:
             break;
         
     }
}



void ac_zero_init(void)
{
    stc_gpio_init_t stcGpioInit = {0};
    
    ///< 外设时钟使能
    SYSCTRL_PeriphClkEnable(SYSCTRL_PERICLK_PB);
    
    stcGpioInit.u32Mode = GPIO_MODE_INPUT | GPIO_MODE_EXTI;
    stcGpioInit.u32Pin  = GPIO_PIN_04;
    stcGpioInit.u32ExtI = GPIO_EXTI_RISING_FALLING;
    GPIOB_Init(&stcGpioInit);

    ///< NVIC 中断使能
    EnableNvic(PORTB_IRQn, IrqLevel0, TRUE);
}


static volatile uint8 zero_flag = 0;

void PortB_IRQHandler(void)
{
    if(GPIO_ExtIrqStateGet(GPIOB,GPIO_PIN_04))
    {
        GPIO_ExtIrqStateClear(GPIOB,GPIO_PIN_04);
    
//        if(GPIO_PB04_READ()){
//            set_led(ON);
//        }else{
//            set_led(OFF);
//        }
        
        zero_flag = 1;
        
    }
}


//开关动作depend on 过零信号(ACZERO)
void test3_task(void)
{
    switch(test_state){
        case TEST_STATE_INIT:
            if(is_key_click()){
                relay_state ^= 0x01;
        
                EnableNvic(PORTB_IRQn, IrqLevel0, FALSE); //关闭中断
                zero_flag = 0; 
                EnableNvic(PORTB_IRQn, IrqLevel0, TRUE); //打开中断
                
                relative_time_add_ms(&test_rtc,50);
                test_state = TEST_STATE_ONE;
                
            }
            break;
        case TEST_STATE_ONE:
            if(is_relative_time_timeout(&test_rtc) || zero_flag)
            {
                set_relay_switch(relay_state);
                set_led(relay_state);
                test_state = TEST_STATE_INIT;
            }
            break;
        default:
            break;
    
    
    }

}



/*
void test_io_interrupt(void)
{
    stc_gpio_init_t stcGpioInit = {0};
    
    SYSCTRL_PeriphClkEnable(SYSCTRL_PERICLK_PD);
    
    ///< LED端口初始化
    stcGpioInit.u32Mode = GPIO_MODE_INPUT | GPIO_MODE_EXTI;
    stcGpioInit.u32Pin  = GPIO_PIN_06;
    stcGpioInit.u32Pull = GPIO_PULL_NONE;
    stcGpioInit.u32ExtI = GPIO_EXTI_RISING_FALLING;
    GPIOD_Init(&stcGpioInit);

     ///< NVIC 中断使能
    EnableNvic(PORTD_IRQn, IrqLevel0, TRUE);
}


void PortD_IRQHandler(void)
{   
    if(GPIO_ExtIrqStateGet(GPIOD,GPIO_PIN_06))
    {
        GPIO_ExtIrqStateClear(GPIOD,GPIO_PIN_06);

        if(GPIO_PD06_READ()){
            set_led(ON);
        }else{
            set_led(OFF);
        }
    }
}
*/
